// pages/category/index.js
import {requestUtil, getBaseUrl} from "../../util/resultUtil";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftMenuList:[],
    rightMenuList: [],
    baseurl: '',
    currentIndex: 0, //当前菜单索引 
    scrollTop:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      let categories = [];
      this.setData({
        baseurl: getBaseUrl(),
        currentIndex: 0,
      });
      this.getCotegories();
  },

  async getCotegories(){
    const resp =await requestUtil({url: '/product/findCategories/', method: 'get'});
    this.categories = resp.data;
    let leftMenuList = this.categories.map((v)=>{
      return v.name;
    });
    let rightMenuList = this.categories[this.data.currentIndex].smallTypeList;
    this.setData({
      leftMenuList,
      rightMenuList,
    })
  },
   //设置另一个方法 让他更新onShow内容
    async getCotegories2(index){
    console.log("excute c2");
    const resp =await requestUtil({url: '/product/findCategories/', method: 'get'});
    this.categories = resp.data;
    let leftMenuList = this.categories.map((v)=>{
      return v.name;
    });
    let rightMenuList = this.categories[index].smallTypeList;
    this.setData({
      leftMenuList,
      rightMenuList,
      currentIndex: index,
    })
  },

  handleMenuLeftItem(e){
    const data = e.currentTarget.dataset;
    let rightMenuList = this.categories[data.index].smallTypeList;
    this.setData({
      rightMenuList,
      currentIndex: data.index,
      scrollTop:0,
    })
    console.log(e);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const app = getApp();
    const index = app.globalDate.index;
    console.log(index , typeof(index), 'onshow');
    if(index >= 0){
      console.log(index);
      this.getCotegories2(index)
      console.log( 'if excute');
      app.globalDate.index = -1;
    }else{
      console.log('else excute');
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})